package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.PromotionStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgProductBookDetailMapper;
import com.ygqh.baby.mapper.YgProductBookMapper;
import com.ygqh.baby.po.YgProductBook;
import com.ygqh.baby.po.YgProductBookExample;
import com.ygqh.baby.service.YgProductBookService;
import com.ygqh.baby.service.task.YgProductBookTaskService;
import com.ygqh.baby.utils.DateConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class YgProductBookServiceImpl implements YgProductBookService {

	@Autowired
	private YgProductBookMapper ygProductBookMapper;
	@Autowired
	private YgProductBookDetailMapper ygProductBookDetailMapper;
	@Autowired
	private YgProductBookTaskService ygProductBookTaskService;

	@Override
	public List<YgProductBook> find() {
		YgProductBookExample example = new YgProductBookExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductBookMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgProductBookExample example = new YgProductBookExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductBookMapper.countByExample(example);
	}

	@Override
	public YgProductBook findById(Long id) {
		return ygProductBookMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgProductBook ygProductBook) {
		ygProductBookMapper.insertSelective(ygProductBook);
		ygProductBookDetailMapper.insertBatch(ygProductBook.getProductBookDetails(), ygProductBook.getId());
		ygProductBookTaskService.addPaidTailJob(ygProductBook);
		return 1;
	}

	@Override
	public int update(YgProductBook ygProductBook) {
		ygProductBookMapper.updateByPrimaryKeySelective(ygProductBook);
		//先删除明细
		ygProductBookDetailMapper.updateStatus(DataStatus.Delete.name(), ygProductBook.getId());
		ygProductBookDetailMapper.insertBatch(ygProductBook.getProductBookDetails(), ygProductBook.getId());
		ygProductBookTaskService.addPaidTailJob(ygProductBook);
		return 1;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgProductBookExample example = new YgProductBookExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgProductBook record = new YgProductBook();
		record.setStatus(DataStatus.Delete);

		return ygProductBookMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgProductBook ygProductBook) {
		int r;
		if (ygProductBook.getId() != null) {
			r = this.update(ygProductBook);
		} else {
			r = this.save(ygProductBook);
		}
		return r;
	}

	@Override
	public ResultSet<YgProductBook> search(QueryInfo queryInfo, Long long1,
										   PromotionStatus pStatus, Date startDate, Date endDate, String q) {
		List<YgProductBook> list = ygProductBookMapper.selectSuper(queryInfo, q);
		int count = ygProductBookMapper.countSuper(q);
		return new ResultSet<>(count, list);

	}

	@Override
	public YgProductBook findBySkuId(Long skuId) {
		if (skuId == null) {
			return null;
		}
		List<YgProductBook> productBookList = ygProductBookMapper.selectBySkuId(skuId, DateConvertUtils.format(new Date()));
		return CollectionUtils.isEmpty(productBookList) ? null : productBookList.get(0);
	}

	@Cacheable(cacheNames = "redis", key = "'product:book_pid_'+#productId")
	@Override
	public YgProductBook findByProductId(Long productId) {
		if (productId == null) {
			return null;
		}
		List<YgProductBook> productBookList = ygProductBookMapper.selectByProductId(productId, DateConvertUtils.format(new Date()));
		return CollectionUtils.isEmpty(productBookList) ? null : productBookList.get(0);
	}

}

